﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Touch2Like.DAL;
using Touch2Like.Model;
using System.Data;
using Touch2Like.SharedLibrary;

namespace Touch2Like.Web.Admin.Popup
{
    public partial class AddUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                tb_UserGUID.Value = Guid.NewGuid().ToString();

                if (!string.IsNullOrEmpty(Request["uname"]))
                {
                    Touch2Like.DAL.UserDAL user = new Touch2Like.DAL.UserDAL();
                    UserInfo info = user.GetUserByNameAndEmail(0, Request["uname"]);

                    if (info != null)
                    {
                        tb_FirstName.Text = info.FirstName;
                        tb_LastName.Text = info.LastName;
                        tb_Email.Text = info.Email;
                        tb_UserGUID.Value = info.UserGuid;
                        tb_Username.Text = info.UserName;
                        tb_Password.Text = Utility.Encryption.Decrypt(info.Password);
                        tb_BusinessName.Text = info.BusinessName;
                        tb_AddressLine1.Text = info.AddressLine1;
                        tb_AddressLine2.Text = info.AddressLine2;
                        tb_City.Text = info.City;
                        ddl_State.SelectedValue = info.State;
                        tb_ZipCode.Text = info.Zipcode;

                        DataTable dtSetting = user.GetSettingByUserGUID(info.UserGuid);

                        if (dtSetting.Select("FlagName='Logo'").Length > 0)
                        {
                            img_Logo.ImageUrl = dtSetting.Select("FlagName='Logo'")[0]["Value"].ToString();
                        }
                    }
                }
            }
        }

        protected void btn_Submit_Click(object sender, EventArgs e)
        {
            Touch2Like.DAL.UserDAL user = new Touch2Like.DAL.UserDAL();
            UserInfo info = new UserInfo();
            if (!string.IsNullOrEmpty(Request["uname"]))
            {
                info = user.GetUserByNameAndEmail(0, Request["uname"]);
                if (info == null) info = new UserInfo();
            }

            info.FirstName = tb_FirstName.Text;
            info.LastName = tb_LastName.Text;
            info.Email = tb_Email.Text;
            info.UserGuid = tb_UserGUID.Value;
            info.UserName = tb_Username.Text.Trim();
            info.Password = Utility.Encryption.Encrypt(tb_Password.Text.Trim());
            info.BusinessName = tb_BusinessName.Text.Trim();
            info.AddressLine1 = tb_AddressLine1.Text;
            info.AddressLine2 = tb_AddressLine2.Text;
            info.City = tb_City.Text;
            info.State = ddl_State.SelectedValue;
            info.Zipcode = tb_ZipCode.Text;

            if (file_Logo.HasFile)
            {
                string path = "/Customer/assets/" + DateTime.Now.Ticks + ".jpg";
                file_Logo.PostedFile.SaveAs(Server.MapPath(path));

                UserSettingInfo settingInfo = new UserSettingInfo();
                settingInfo.UserGUID = info.UserGuid;
                settingInfo.FlagName = "Logo";
                settingInfo.Value = path;

                user.UpdateSetting(settingInfo);
            }

            if (!string.IsNullOrEmpty(Request["uname"]))
            {
                user.UpdateUser(info, "UPDATE");
            }
            else
            {
                user.UpdateUser(info, "ADD");
            }

            ClientScript.RegisterStartupScript(this.GetType(), "update", "<script>parent.document.location.href=parent.document.location.href;parent.document.location.reload();</script>");
        }
    }
}